package com.ruoyi.crm.sale.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.sale.mapper.CrmBusinessMapper;
import com.ruoyi.crm.sale.domain.CrmBusiness;
import com.ruoyi.crm.sale.domain.CrmBusinessProduct;
import com.ruoyi.crm.sale.domain.CrmCustomer;
import com.ruoyi.crm.sale.domain.CrmCustomerContacts;
import com.ruoyi.crm.sale.domain.CrmTeam;
import com.ruoyi.crm.sale.service.ICrmBusinessService;
import com.ruoyi.crm.sale.service.ICrmCustomerContactsService;
import com.ruoyi.crm.sale.service.ICrmCustomerService;
import com.ruoyi.crm.sale.service.ICrmTeamService;

/**
 * 商机Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class CrmBusinessServiceImpl implements ICrmBusinessService 
{
    @Autowired
    private CrmBusinessMapper crmBusinessMapper;
    @Autowired
    private ICrmTeamService crmTeamService;
    @Autowired
    private ICrmCustomerService crmCustomerService;
    @Autowired
    private ICrmCustomerContactsService crmCustomerContactsService;

    /**
     * 查询商机
     * 
     * @param id 商机ID
     * @return 商机
     */
    @Override
    public CrmBusiness selectCrmBusinessById(Long id)
    {
        return crmBusinessMapper.selectCrmBusinessById(id);
    }

    /**
     * 查询商机-产品
     * 
     * @param id 商机ID
     * @return 商机
     */
    @Override
    public CrmBusiness getCrmBusProductById(CrmBusinessProduct busPro)
    {
        return crmBusinessMapper.selectCrmBusProductById(busPro);
    }

    /**
     * 查询商机列表
     * 
     * @param crmBusiness 商机
     * @return 商机
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "b")
    public List<CrmBusiness> selectCrmBusinessList(CrmBusiness crmBusiness)
    {
        return crmBusinessMapper.selectCrmBusinessList(crmBusiness);
    }

    /**
     * 新增商机
     * 
     * @param crmBusiness 商机
     * @return 结果
     */
    @Override
    public int insertCrmBusiness(CrmBusiness crmBusiness)
    {
    	crmBusiness.setCreateTime(DateUtils.getNowDate());
    	crmBusiness.setUpdateTime(DateUtils.getNowDate());
    	crmBusiness.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    	
    	if(null == crmBusiness.getCustomerId()) {
    		CrmCustomer customer = new CrmCustomer();
    		customer.setCustomerName(crmBusiness.getCustomerName());
    		customer.setCustomerSource(crmBusiness.getBusSource());
    		customer.setLeader(crmBusiness.getBusinessLeader());
    		customer.setCustomerLevel(crmBusiness.getBusLevel());
    		customer.setCustomerPerson(crmBusiness.getFirstContacts());
    		customer.setTel(crmBusiness.getFirstTel());
    		customer.setMail(crmBusiness.getFirstMail());
    		crmCustomerService.insertCrmCustomer(customer);
    		crmBusiness.setCustomerId(customer.getId());
    	}
    	
    	
    	int id = crmBusinessMapper.insertCrmBusiness(crmBusiness);
    	
    	//添加商机负责人
    	if(null != crmBusiness.getBusinessLeader() && crmBusiness.getBusinessLeader() != 0) {
    		CrmTeam crmTeam = new CrmTeam();
    		crmTeam.setUserId(crmBusiness.getBusinessLeader());
    		crmTeam.setBusinessId(crmBusiness.getId());
    		crmTeam.setIsLeader((long)1);//负责人
    		crmTeam.setTeamType((long)2);//团队类型 1：客户  2：商机
    		crmTeamService.setLeader(crmTeam);
    	}
    	
    	//添加商机首要联系人
    	if(null != crmBusiness.getFirstContacts() && !"".equals(crmBusiness.getFirstContacts())) {
        	CrmCustomerContacts contacts = new CrmCustomerContacts();
        	contacts.setBusId(crmBusiness.getId());
        	contacts.setType("3");//商机
        	contacts.setName(crmBusiness.getFirstContacts());
        	contacts.setTel(crmBusiness.getFirstTel());
        	contacts.setEmail(crmBusiness.getFirstMail());
        	crmCustomerContactsService.insertCrmCustomerContacts(contacts);
    	}
    	
        return id;
    }

    /**
     * 修改商机
     * 
     * @param crmBusiness 商机
     * @return 结果
     */
    @Override
    public int updateCrmBusiness(CrmBusiness crmBusiness)
    {
    	crmBusiness.setUpdateTime(DateUtils.getNowDate());
    	crmBusiness.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    	
    	if(null != crmBusiness.getBusinessLeader() && crmBusiness.getBusinessLeader() != 0) {
    		CrmTeam crmTeam = new CrmTeam();
    		crmTeam.setUserId(crmBusiness.getBusinessLeader());
    		crmTeam.setBusinessId(crmBusiness.getId());
    		crmTeam.setIsLeader((long)1);//负责人
    		crmTeam.setTeamType((long)2);//团队类型 1：客户  2：商机
    		crmTeamService.setLeader(crmTeam);
    	}
    	
        return crmBusinessMapper.updateCrmBusiness(crmBusiness);
    }

    /**
     * 修改商机阶段
     * 
     * @param crmBusiness 商机
     * @return 结果
     */
    public int updateBusinessStage(CrmBusiness crmBusiness)
    {
    	crmBusiness.setUpdateTime(DateUtils.getNowDate());
    	crmBusiness.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        return crmBusinessMapper.updateBusinessStage(crmBusiness);
    }

    /**
     * 批量删除商机
     * 
     * @param ids 需要删除的商机ID
     * @return 结果
     */
    @Override
    public int deleteCrmBusinessByIds(Long[] ids)
    {
        return crmBusinessMapper.deleteCrmBusinessByIds(ids);
    }

    /**
     * 删除商机信息
     * 
     * @param id 商机ID
     * @return 结果
     */
    @Override
    public int deleteCrmBusinessById(Long id)
    {
        return crmBusinessMapper.deleteCrmBusinessById(id);
    }

    /**
     * 查询有效商机数量
     * 
     * @return 结果
     */
    public int selBusCount(Map<String, Object> params) {
    	return crmBusinessMapper.selBusCount(params);
    }

    /**
     * 商机阶段销售漏斗 
     * 
     * @return 结果
     */
    public List<Map<String, Object>> funnelCount(Map<String, Object> params){
    	return crmBusinessMapper.funnelCount(params);
    }
}
